#ifndef IMAGEITEM_H
#define IMAGEITEM_H

#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QGraphicsSceneMouseEvent>
#include <QStyleOptionGraphicsItem>
#include <QFocusEvent>
#include <QList>
#include <QPointF>
#include <QPainter>

#include "focusitem.h"
#include "imageitemproxy.h"

#include "Defs.h"

class ImageItem : public QGraphicsPixmapItem
{
public:
    enum MouseMode{MOVE, TRANSFORM, SELECT};
    enum { Type = UserType + 100 };
    enum TransformType{RESIZE, ROTATE, FREE};

    ImageItem(QGraphicsItem *parent = 0);

    virtual int type() const {return Type;}

    QRectF boundingRect() const;

    void setPixmap(const QPixmap &pixmap);

    void showFocusItems(bool visible);
    void setMode(MouseMode mode);
    void setTransformType(TransformType type){m_type = type;}
    MouseMode getMode(){return m_mode;}

    ImageItemProxy* getProxy() {return m_proxy;}

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    void focusOutEvent(QFocusEvent *event);
    void hoverMoveEvent(QGraphicsSceneHoverEvent * event);
    void hoverLeaveEvent(QGraphicsSceneHoverEvent *);
    void hoverEnterEvent(QGraphicsSceneHoverEvent *);

private:
    void createFocusItems();
    void updateSelectedImage();

    qreal m_width, m_originWidth;
    qreal m_height, m_originHeight;
    qreal m_margin;

    bool m_selected;

    MouseMode m_mode;
    TransformType m_type;

    QList<FocusItem*> m_focusItems;
    FocusItem* m_curFocusItem;
    QPointF m_lastPoint, m_originPoint;
    QGraphicsRectItem* m_dashRect;
    qreal m_degree, m_tempDegree;

    ImageItemProxy* m_proxy;
};

#endif // IMAGEITEM_H
